﻿using Furion;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Huasky.Gm.EntityFramework.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDatabaseAccessor(options =>
        {
            options.AddDbPool<GmDbContext>();
        }, "Huasky.Gm.Database.Migrations");
    }

    public void Configure(IApplicationBuilder app, IHostEnvironment env)
    {
        Scoped.Create((_, scope) =>
        {
            // 自动迁移（每次实体更改后，在程序包管理器控制台执行Add-Migration，生成迁移文件）
            var context = scope.ServiceProvider.GetRequiredService<GmDbContext>();
            //context.Database.EnsureCreated();
            context.Database.Migrate();

        });
    }
}
